package org.hk.proxy.protocol;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;

public class HeartBeatHandler extends IdleStateHandler {
    public HeartBeatHandler(int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds) {
        super(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds);
    }
    @Override
    protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
        if(IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT == evt){
            ProxyMessage proxyMessage = new ProxyMessage();
            proxyMessage.setType(ProxyMessage.HEART_PACKAGE);
            ctx.writeAndFlush(proxyMessage);
            }else if(IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT == evt){
            ctx.channel().close();
        }
    }
}
